//
//  Modify_phoneNum_step_two_vc.m
//  ainkjj
//
//  Created by zhiyiLiu on 2020/3/2.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Modify_phoneNum_step_two_vc.h"
#import "JKCountDownButton.h"
#import "Set_login_pwd_vc.h"

@interface Modify_phoneNum_step_two_vc ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UITextField *codeTf;
@property (weak, nonatomic) IBOutlet JKCountDownButton *get_checkCode_btn;

@end

@implementation Modify_phoneNum_step_two_vc
- (IBAction)regAction:(UIButton *)sender {
    // 校验验证码/api/Auth/checkCode
    
    
    if(self.phoneTf.text.length != 11){
        [MBProgressHUD showTipMessageInView:@"请输入11位手机号"];
        return;
    }
    if (self.codeTf.text.length != 6) {
        [MBProgressHUD showTipMessageInView:@"请输入6位数验证码"];
        return;
    }
    
   
//    mobile=18162152664&verification=395417&app_code=ainankang
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:app_code forKey:@"appCode"];
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"token"];
    [dict setObject:[Anktool strBlankHolder:self.phoneTf.text] forKey:@"phone"];
    [dict setObject:self.codeTf.text forKey:@"code"];
    
    sender.userInteractionEnabled = NO;
    [PPHTTPRequest req:@"java/app/user/updatePhone" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            AuthSignin_model.sharedInstance.data.user_info.mobile = self.phoneTf.text ;
            NSString *dictStr = [AuthSignin_model.sharedInstance mj_JSONString];
            [[NSUserDefaults standardUserDefaults] setObject:dictStr forKey:@"ank_sign"];
            [QMUITips showSucceed:@"绑定成功"];
            
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }
        else{
            
            [QMUITips showError:response[@"error_msg"]];
            
        }
        sender.userInteractionEnabled = YES;
    } failure:^(NSError *error) {
        [QMUITips showError:[error localizedDescription]];
        sender.userInteractionEnabled = YES;
    }];

}

- (IBAction)get_code_action:(JKCountDownButton *)sender {
    if(self.phoneTf.text.length != 11){
        [MBProgressHUD showTipMessageInView:@"请输入11位手机号"];
        return;
    }
    
        
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[Anktool strBlankHolder:self.phoneTf.text] forKey:@"mobile"];
    [dict setObject:app_code forKey:@"app_code"];
    [dict setObject:channel_type forKey:@"channel_type"];
    [dict setObject:@"0" forKey:@"code_type"];

    
    [PPHTTPRequest req:@"api/Auth/sms" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            [MBProgressHUD showTipMessageInView:@"发送成功"];
            [self.codeTf becomeFirstResponder];
            sender.enabled = NO;
            //button type要 设置成custom 否则会闪动
            [sender startCountDownWithSecond:60];
            
            [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
                NSString *title = [NSString stringWithFormat:@"剩余%zd秒",second];
                return title;
            }];
            [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
                countDownButton.enabled = YES;
                return @"点击重新获取";
                
            }];
            
            
        }
        else
//            if ([error_code isEqualToString:@""])
            {
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
        
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    
}
- (void)initSubviews{
    [super initSubviews];
    self.title = @"绑定新手机号";
    
    self.codeTf.delegate =  self.phoneTf.delegate = self;
    self.get_checkCode_btn.layer.borderWidth = 1;
    self.get_checkCode_btn.layer.borderColor = themeColor.CGColor;
    
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSMutableString *newtxt = [NSMutableString stringWithString:textField.text];
    
    
    [newtxt replaceCharactersInRange:range withString:string];
    
    // 客户端仅仅判断手机号是否11位
    if ([textField isEqual:self.phoneTf]) {// 手机号11位
        int MAX_CHARS = 11;
        
        return ([newtxt length] <= MAX_CHARS);
        
    }else if([textField isEqual:self.codeTf]){
        int MAX_CHARS = 6;
        
        return ([newtxt length] <= MAX_CHARS);
    }
    
    
    return YES;
    
}

@end
